# -*- coding: utf-8 -*-
# @Author   : niejiali
# @Time     : 2019/2/23 14:14
# @File     : __contextmanger011.py
# @Software : PyCharm

from contextlib import contextmanager

@contextmanager
def opened(name):
    f = open(name)
    try:
        yield f
    finally:
        f.close()

with opened('/tmp/a.txt') as f:
    for line in f.readlines():
        print(line)